/**
 * 医疗服务质询初始化模块，提供医疗服务编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","msgServ","flowInit","datagridServ","buttonEvent"],
		function($,sysmanMain,msgServ,flowInit,datagridServ,buttonEvent) {
			
		'use strict';
		var _typeUrl = '/dictionary/getTree.action',_rootpath,
			el_width = 300,el_big_width=674,window_width=900,window_height=700,
			
			rootid = 'panel-medical-recipel',
			edit_window_id = 'medical-recipel-win'
			
		var public_method ={
			
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId	页面根节点id
			 * @param _options.flowStateSelector	流程状态字段选择器
			 */
			initElement:function(_options){
				
				if(window.console){
					console.info(' recipel init loaded and startting ...')
				}
				
			},
			closeWindow:function(){
				$('#medical-recipel-win').empty()
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				_moduleConfig.initParam = {editable:true};
				sysmanMain.setCurrentModuConfig(_moduleConfig);
			},
			/**
			 * @param _options.rootId
			 * @param _options.conditionSelector
			 * @param _options.
			 */
			initList:function(_options){
				private_method.initCondition(_options)
			},
			/**
			 * @param _options.record
			 */
			buttonExtends:function(){
				return {'查看详情':private_method.detailInfo}
			},
			
			reCreateField:function(_options){
				private_method.reCreateField(_options)
			},
			rowDblClick:function(index,row){
				private_method.rowDblClick(index,row)
			}
			
		}
		
		var private_method = {
			rowDblClick:function(index,row){
				var rootId = 'panel-medical-recipel';
				var editWindow = 'medical-recipel-win';
				
				var _param = {
					index:index,
					row:row,
					rootId:'panel-medical-recipel',
					editWindow:'medical-recipel-win',
					//moduleConfig:sysmanMain.getCurrentModuConfig(),传入空值，自动取当前模块配置
					url:'/medicalRecipel/query/editPage.action',
					width:window_width,
					height:window_height
				}
				buttonEvent.doubleClickEdit(_param)
			},
			/**
			 * 初始化条件查询框
			 * @param _options.rootId 窗口根节点id
			 * @param _options.conditionSelector 条件框节点选择器
			 */
			initCondition:function(_options){
				if(window.console){
					console.info('recipel init loaded and starting...')
				}
				
				flowInit.initList(_options)
			},
			detailInfo:function(e){
				if(window.console){
					console.info('show detail loaded...')
				}
				var _config ={}
				
				
				//获取患者类型
				var patient_type = $('#medical-recipel-win form.main-form #patient_type').val()
				
				switch(patient_type){
						case "1":
							//门诊
							var outpatientElement = $('#medical-recipel-win form.main-form #patient_id')
							
							var _patient_id
							if(outpatientElement && outpatientElement.length>0){
								_patient_id = $(outpatientElement).val();
								
								//判断处方号是否为空
								if(_patient_id){
									_config.initQueryParams = {condition:{outpatient_number:_patient_id, prescribe_code_like:'cf'}}
									sysmanMain.setModuConf('outpatientItemFee',_config)
									sysmanMain.medicalOutpatientItemFeeSelect()
								}
									
							}
							
							
							
							break;
						case "2":
							
							//获取患者编号
							var inpatientElement = $('#medical-recipel-win form.main-form #patient_id')
							
							var _patient_id
							if(inpatientElement && inpatientElement.length>0){
								_patient_id = $(inpatientElement).textbox('getValue')
								
								//判断住院号是否为空
								if(_patient_id){
									_config.initQueryParams = {condition:{patient_id:_patient_id}}
									sysmanMain.setModuConf('inpatientItemFee',_config)
									sysmanMain.medicalInpatientItemFeeSelect()
								}
							}
							//住院
							
							break;
						default:
							//获取患者编号
							var inpatientElement = $('#medical-recipel-win form.main-form #patient_id')
							
							var _patient_id
							if(inpatientElement && inpatientElement.length>0){
								_patient_id = $(inpatientElement).textbox('getValue')
								
								//判断住院号是否为空
								if(_patient_id){
									_config.initQueryParams = {condition:{patient_id:_patient_id}}
									sysmanMain.setModuConf('inpatientItemFee',_config)
									sysmanMain.medicalInpatientItemFeeSelect()
								}
							}
					}				
					
//					msgServ.Msgslide("请先选择 院内编号")						
				if(e && e.currentTarget){
					$(e.currentTarget).linkbutton('enable');
				}
				
			},
			/**
			 * 更新质询子类信息
			 * @param _options.form	
			 * @param _options.record
			 * @param _options.target
			 */
			changeSubItem:function(_options){
				var _subSelect = $(_options.form).find('#inquiry_item_sub')
				$(_subSelect).combobox({
					textField:'name',
					valueField:'name',
					readonly:false,
					url:_rootpath + _typeUrl+'?id=' + _options.record.id,
					onSelect:function(record){
						var _item = $(_options.form).find('#inquiry_item_subcode')
						$(_item).val(record.id)
					}
				})	
				
			},
			
			//改造函数
			reCreateField:function(_options){
				if(window.console){
					console.info(' recipel reCreateField loaded and startting ...')
				}
				var normal_width_param = {width:el_width}
				var big_width_param = $.extend({},{width:el_big_width,multiline:true,height:50})
				var $form = _options.form
				
				_rootpath = '/'+sysmanMain.getRootPath()
				
				var fConfig = {
					bizno:$.extend({fieldType:'textbox'},normal_width_param),	
					patient_id:$.extend({fieldType:'textbox'},normal_width_param),
					inquiry_date:$.extend({fieldType:'datebox'},normal_width_param),
					inquiry_reason:$.extend({fieldType:'textbox'},big_width_param),
					litigant_content:$.extend({fieldType:'textbox'},big_width_param),
					decide_content:$.extend({fieldType:'textbox'},big_width_param),
					affirm_content:$.extend({fieldType:'textbox'},big_width_param),
					zyhm:$.extend(
						{
							fieldType:'textbox',
							onClickButton:function(data){
								var $dept_window = $('#win-dept-patient');
								
								$($dept_window).attr('callback-id','medical-recipel-win #bizid')
								$($dept_window).attr('callback-patient_id','medical-recipel-win #patient_id')
								$($dept_window).attr('callback-zyhm','medical-recipel-win #zyhm')
								$($dept_window).attr('callback-doct_name','medical-recipel-win #doct_name')
								$($dept_window).attr('callback-doct_id','medical-recipel-win #doct_id')
								$($dept_window).attr('callback-doct_depart_name','medical-recipel-win #doct_depart_name')
								$($dept_window).attr('callback-doct_depart_code','medical-recipel-win #doct_depart_code')
								$($dept_window).attr('callback-patient_name','medical-recipel-win #patient_name')
								sysmanMain.deptPatientSelect()
							}
						}
						,
						normal_width_param
					),
					
					inquiry_item:$.extend(
						{fieldType:'combobox'},
						{
							textField:'name',
							valueField:'name',
							url:_rootpath + _typeUrl+'?id=' + 4,
							onSelect:function(record){
								var _item = $(_options.form).find('#inquiry_item_code')
								$(_item).val(record.id)
								private_method.changeSubItem({form:_options.form,record:record})
							},
							onLoadSuccess:function(){
								if(selectedRecord && selectedRecord.inquiry_item_code){
//										$(_mainSelect).combobox('select',selectedRecord.inquiry_item)
									private_method.changeSubItem({form:_options.form,record:{id:selectedRecord.inquiry_item_code}})
								}
							}
						},
						normal_width_param),
					
						
						
						decide_result:$.extend(
							{fieldType:'combobox'},
							{
								textField:'name',
								valueField:'name',
								url:_rootpath + _typeUrl+'?id=' + 106,
								onSelect:function(record){
									var _item = $(_options.form).find('#decide_result_code')
									$(_item).val(record.id)
									
									var $username = $(_options.form).find('#decide_name')
									var $userid = $(_options.form).find('#decide_id')
									
									var curUser = sysmanMain.getCurrentUser()
									$($username).textbox('setValue',curUser.name)
									$($userid).val(curUser.id)
								}
							},
							normal_width_param),
					affirm_result:$.extend(
							{fieldType:'combobox'},
							{
								textField:'name',
								valueField:'name',
								url:_rootpath + _typeUrl+'?id=' + 112,
								onSelect:function(record){
									var _item = $(_options.form).find('#affirm_result_code')
									$(_item).val(record.id)
									
									var $username = $(_options.form).find('#affirm_name')
									var $userid = $(_options.form).find('#affirm_id')
									
									var curUser = sysmanMain.getCurrentUser()
									$($username).textbox('setValue',curUser.name)
									$($userid).val(curUser.id)
								}
							},
							normal_width_param)
				}
				
				flowInit.initField({
					fieldConfig:fConfig,
					fieldStatus:_options.data,
					window_id:edit_window_id
				})
					
			
			}
			
									
				
		}	
		
		return public_method;
			
})